package top.zywork.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.zywork.common.BindingResultUtils;
import top.zywork.query.wx.SchoolOrderWxQuery;
import top.zywork.security.SecurityUtils;
import top.zywork.service.SchoolOrderService;
import top.zywork.vo.ResponseStatusVO;
import top.zywork.vo.wx.SchoolOrderPlaceVo;
import top.zywork.vo.wx.SchoolUserOrderWxVO;
import top.zywork.wx.query.CourierHallQuery;
import top.zywork.wx.response.CourierHallResponse;

import java.util.List;

/**
 * <p>
 *  快递代拿前端控制器
 * </p>
 *
 * @author
 * @since 2020-10-13
 */
@RestController
@RequestMapping("/school-order")
@Slf4j
public class SchoolOrderController {

    @Autowired
    private SchoolOrderService schoolOrderService;

    /**
     * 用户快递代取下单
     * @param schoolOrderPlaceVo
     * @return
     */
    @PostMapping("user/place")
    public ResponseStatusVO place(@RequestBody @Validated SchoolOrderPlaceVo schoolOrderPlaceVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            return ResponseStatusVO.dataError(BindingResultUtils.errorString(bindingResult), null);
        }
        return schoolOrderService.placeOrder(schoolOrderPlaceVo);
    }

    /**
     * 查询当前用户订单列表
     * @return
     */
    @PostMapping("user/order")
    public ResponseStatusVO order(@RequestBody SchoolOrderWxQuery schoolOrderWxQuery){
        IPage<SchoolUserOrderWxVO> order =schoolOrderService.order(schoolOrderWxQuery);
        return ResponseStatusVO.ok("查询成功",order);
    }

    /**
     * 用户订单详情 订单表、接单人用户信息、地址
     * @return
     */
    @GetMapping("user/orderDetails/{orderId}")
    public ResponseStatusVO orderDetails(@PathVariable("orderId") String orderId){
        return schoolOrderService.orderDetails(orderId);
    }

    /**
     * ----------------------------需求大厅相关
     */

    /**
     * 快递代拿需求大厅列表
     * @param courierHallQuery
     * @return
     */
    @PostMapping("any/courier/hall")
    public ResponseStatusVO courierHall(@RequestBody @Validated CourierHallQuery courierHallQuery,BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            return ResponseStatusVO.dataError(BindingResultUtils.errorString(bindingResult), null);
        }
        long userId = SecurityUtils.getJwtUser().getUserId();
        // TODO 控制下单人看到自己的订单
        courierHallQuery.setUserId(userId);
        List<CourierHallResponse> courierHallResponses = schoolOrderService.courierHall(courierHallQuery);
        return ResponseStatusVO.ok("查询成功",courierHallResponses);
    }

    /**
     * 快递代拿需求大厅接单
     * @return
     */
    @PostMapping("user/courier/order/{orderId}")
    public ResponseStatusVO courierOrder(@PathVariable("orderId") String orderId){
        return schoolOrderService.courierOrder(orderId);
    }

    /**
     * 接单人拿到快递之后修改订单状态 为配送中
     */
    @GetMapping("user/orderUser/distribution/{orderId}")
    public ResponseStatusVO orderUserDistribution(@PathVariable("orderId") String orderId){
        return schoolOrderService.orderUserDistribution(orderId);
    }

    /**
     * 下单人拿到快递之后修改订单状态 为已完成
     * @param orderId
     * @return
     */
    @GetMapping("user/user/distribution/{orderId}")
    public ResponseStatusVO userDistribution(@PathVariable("orderId") String orderId){
        return schoolOrderService.userDistribution(orderId);
    }
}
